package com.example.shopspring.controller;

import com.example.shopspring.common.ApiConstant;
import com.example.shopspring.common.ResultData;
import com.example.shopspring.dto.AdminInDto;
import com.example.shopspring.dto.UpdatePasswordInDto;
import com.example.shopspring.entity.Admin;
import com.example.shopspring.service.AdminService;
import com.example.shopspring.utils.JWTUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(ApiConstant.API_VERSION + "/admin")
@Slf4j
public class AdminController {

    @Resource
    private AdminService adminService;

    /**
     * 登录接口
     *
     * @param adminInDto 登录实体类
     */
    @PostMapping("/login")
    public ResultData login(@RequestBody AdminInDto adminInDto) {
        log.info("登录");
        return adminService.login(adminInDto);
    }


    //获取用户信息
    @GetMapping("/get_info")
    public ResultData getInfo(HttpServletRequest request) {
        String token = request.getHeader("token");
        Admin info = adminService.getAdminInfo(token);
        return ResultData.success(info);
    }

    @PostMapping("/update_password")
    public ResultData updatePassword(HttpServletRequest request, @RequestBody UpdatePasswordInDto passwordInDto){
       return adminService.updatePassword(request.getHeader("token"),passwordInDto);
    }

    @PostMapping("/logout")
    public ResultData logout(HttpServletRequest request) {
        String token = request.getHeader("token");
        JWTUtil.invalidateToken(token);
        return ResultData.success("退出成功");
    }
}
